#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"

//设置模式显示
void Set_Display_ModeState(uint8_t ModeSate);

/* widgets:
Layer_KT_ModeSet
Background_KTModeSet
PopupButton_ModeSetBack
Container_ModeSet_On
PopupButton_ModeFlash_On
PopupButton_ModeHum_On
PopupButton_ModeHot_On
PopupButton_ModeCold_On
Container_ModeSet_Off
Animation_ModeFlash_Off
PopupButton_ModeFlash_Off
PopupButton79
Animation_ModeHum_Off
PopupButton_ModeHum_Off
PopupButton83
PopupButton84
Animation_ModeHot_Off
PopupButton_ModeHot_Off
PopupButton70
PopupButton71
PopupButton72
Animation_ModeCold_Off
PopupButton_ModeCold_Off
PopupButton60
PopupButton61
PopupButton62
PopupButton63
*/
static ITUPopupButton* PopupButton_ModeCold_On;
static ITUPopupButton* PopupButton_ModeHot_On;
static ITUPopupButton* PopupButton_ModeHum_On;
static ITUPopupButton* PopupButton_ModeFlash_On;
static uint8_t Mode_Temp;
static uint8_t DwellTime = 0;
bool KTModeSetOnEnter(ITUWidget* widget, char* param)
{
	if (!PopupButton_ModeCold_On)
	{
		PopupButton_ModeCold_On = ituSceneFindWidget(&theScene, "PopupButton_ModeCold_On");
		assert(PopupButton_ModeCold_On);
		PopupButton_ModeHot_On = ituSceneFindWidget(&theScene, "PopupButton_ModeHot_On");
		assert(PopupButton_ModeHot_On);
		PopupButton_ModeHum_On = ituSceneFindWidget(&theScene, "PopupButton_ModeHum_On");
		assert(PopupButton_ModeHum_On);
		PopupButton_ModeFlash_On = ituSceneFindWidget(&theScene, "PopupButton_ModeFlash_On");
		assert(PopupButton_ModeFlash_On);
	}
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Mode)
		Set_Display_ModeState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Mode);
	else
		Set_Display_ModeState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Mode);
	Set_Rotate_Block(360);//旋转步进
    return true;
}

bool KTModeSetOnLeave(ITUWidget* widget, char* param)
{
	if (Mode_Temp != TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Mode)//状态有改变
	{
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Mode = Mode_Temp;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Mode = Mode_Temp;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.status = 0x56;//设置模式
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Mode = Mode_Temp;
		printf("Moorgen INFO : Touch to set the air conditioner's mode.\n");
		RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	}
	DwellTime = 0;
	MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
    return true;
}

bool KTModeSetOnTimer(ITUWidget* widget, char* param)
{
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
		Clear_User_Action();
	//外部事件状态更新//设备开处理旋转动作
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.status == 1)
	{
		if (Action_Cmd.User_Action == Clockwise_Rotation)//顺时针旋转
		{
			DwellTime = 0;
			if (Mode_Temp < 4)
			{
				Mode_Temp++;
				Set_Display_ModeState(Mode_Temp);
			}
			Clear_User_Action(); 
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation)//逆时针旋转
		{
			DwellTime = 0;
			if (Mode_Temp > 1)
			{
				Mode_Temp--;
				Set_Display_ModeState(Mode_Temp);
			}
			Clear_User_Action(); 
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		}
	}
	if (DwellTime < 250)
		DwellTime++;
	else
	{
		ITULayer *Layer;
		DwellTime = 0;
		Layer = ituSceneFindWidget(&theScene, "Layer_KongTiao");
		assert(Layer);
		ituLayerGoto(Layer);
	}
	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		//根据状态显示模式
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Mode)
			Set_Display_ModeState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Mode);
		else
			Set_Display_ModeState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Mode);
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
}

bool PopupButton_KT_ModeSetCode_Press(ITUWidget* widget, char* param)
{
	Mode_Temp = 1;
	return true;
}
bool PopupButton_KT_ModeSetHum_Press(ITUWidget* widget, char* param)
{
	Mode_Temp = 2;
    return true;
}
bool PopupButton_KT_ModeSetFrash_Press(ITUWidget* widget, char* param)
{
	Mode_Temp = 3;
	return true;
}
bool PopupButton_KT_ModeSetHot_Press(ITUWidget* widget, char* param)
{
	Mode_Temp = 4;
    return true;
}

//显示风速
void Set_Display_ModeState(uint8_t FanSate)
{
	Mode_Temp = FanSate;
	if (Mode_Temp > 4 || Mode_Temp == 0)return;
	//根据状态显示风速
	ituWidgetSetVisible(PopupButton_ModeCold_On, false);
	ituWidgetSetVisible(PopupButton_ModeHot_On, false);
	ituWidgetSetVisible(PopupButton_ModeHum_On, false);
	ituWidgetSetVisible(PopupButton_ModeFlash_On, false);
	if (Mode_Temp == 1)
	{
		//自动
		ituWidgetSetVisible(PopupButton_ModeCold_On, true);
	}
	else if (Mode_Temp == 2)
	{
		//低风
		ituWidgetSetVisible(PopupButton_ModeHum_On, true);
	}
	else if (Mode_Temp == 3)
	{
		//中风
		ituWidgetSetVisible(PopupButton_ModeFlash_On, true);
	}
	else if (Mode_Temp == 4)
	{
		//高风
		ituWidgetSetVisible(PopupButton_ModeHot_On, true);
	}
}
